<template>
  <div class="home-box">
    <el-form :inline="true" :model="searchForm">
      <el-form-item label="员工工号" prop="id">
        <el-input v-model="searchForm.empId" placeholder="员工工号" :min="0" />
      </el-form-item>
      <el-form-item label="员工姓名">
        <el-input
          v-model="searchForm.empName"
          placeholder="员工姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 18.75rem">
		  
		<el-date-picker
		  v-model="searchForm.time"
		  type="month"
		  placeholder="选择月"
				value-format="YYYY-MM"
				:disabledDate="check">
		</el-date-picker>
		
      </el-form-item>
      <el-form-item>
        <el-button @click="searchEvent" type="primary">查询 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="update" type="primary">重置 </el-button>
      </el-form-item>
      <el-form-item >
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button circle icon="Refresh" @click="update" />
        </el-tooltip>
      </el-form-item>
    </el-form>
  </div>
    <el-table
      :data="ScoreRuleList"
      border
      style="width: 95%; margin: 20px auto;border-radius: 8px; margin: 20px auto;box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);"
      :header-cell-style="{
          'background': '#337ecc !important',
          'color': '#ffffff',
          'border': 'none !important'
        }">
      
    
	  
	  <el-table-column prop="empName" label="被考核人" width="" />
	  <el-table-column prop="position" label="岗位名称" width="" />
	  <el-table-column prop="deptName" label="部门" width="" />
	  <el-table-column prop="type" label="类型" width="170"/>
	  <el-table-column prop="target" label="考核条目" width="170"/>
	  <el-table-column prop="scorePercent" label="考核条目占比%" width="100"/>
	  <el-table-column prop="assessorName" label="评分人" width="80"/>
	  <el-table-column prop="assessorPercent" label="评分人占比%" width="110"/>
	  <el-table-column prop="score" label="评分" width="80"/>
	  <el-table-column prop="grade" label="得分" width="110"/>
	  <el-table-column prop="remark" label="评分说明" width="120"/>
	  
	  <!-- <el-table-column prop="originalScore" label="原分值/￥" width="90" v-auth="'编辑评分'"/> -->
	  <!-- <el-table-column prop="correctedValue" label="修正值/￥" width="90" v-auth="'编辑评分'"/> -->
      <!-- <el-table-column prop="correctedValue" label="修正值/￥" width="90"/> -->
    </el-table>
    <pagination
    class="pagination"
    v-show="ScoreListPage.total > 0"
    :total="ScoreListPage.total"
    v-model:page="searchForm.page"
    v-model:limit="ScoreListPage.size"
    @pagination="handlePagination"
  />
</template>

<script setup>
import { ref, onBeforeMount, reactive, getCurrentInstance,watch } from "vue";
import {getEmpScorePastPage,reDelete,getEmpScorePastSearch} from "@/api/empScore"
import { ElMessage } from "element-plus";

const betweenTime=ref()
const searchForm = ref({
  empId: "",
  empName: "",
  page: 1,
  page_size: 10,
  time: "",
});


const ScoreListPage = ref([]);
const ScoreRuleList = ref([]);

watch(searchForm.value, (newValue, oldValue) => {
	searchEvent()
}, { deep: true });


onBeforeMount(() => {
  getEmpScorePastPage(searchForm.value).then((data) => {
	  let item = data.data.records
	  for(var i = 0;i <item.length;i++){
	  	if(item[i].correctedValue == null ){
	  		item[i].grade = (Number(item[i].score)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
	  	}else{
	  		item[i].grade = (Number(item[i].correctedValue)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
	  	}
	  	item[i].scorePercent += "%"
	  	item[i].assessorPercent += "%"
	  }
    ScoreListPage.value = data.data;
    ScoreRuleList.value = data.data.records;
  });
});
watch(
  ScoreRuleList,
  (newVal) => {
    
  },
  { deep: true }
);

const searchEvent=()=>{
  // if(betweenTime.value!=""){
  //   searchForm.value.beginTime=betweenTime.value[0]
  //   searchForm.value.endTime=betweenTime.value[1]
  // }
  
  getEmpScorePastSearch(searchForm.value).then((data) => {
	  let item = data.data.records
	  for(var i = 0;i <item.length;i++){
	  	if(item[i].correctedValue == null ){
	  		item[i].grade = (Number(item[i].score)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
	  	}else{
	  		item[i].grade = (Number(item[i].correctedValue)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
	  	}
	  	item[i].scorePercent += "%"
	  	item[i].assessorPercent += "%"
	  }
    ScoreListPage.value = data.data;
    ScoreRuleList.value = data.data.records;
	
  });
}

const handlePagination=(value)=>{
  if(searchForm.value.empId==""&&searchForm.value.empName==""){
    getEmpScorePastPage(searchForm.value).then((data) => {
		let item = data.data.records
		
		for(var i = 0;i <item.length;i++){
			if(item[i].correctedValue == null ){
				item[i].grade = (Number(item[i].score)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
			}else{
				item[i].grade = (Number(item[i].correctedValue)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
			}
			item[i].scorePercent += "%"
			item[i].assessorPercent += "%"
		}
        ScoreListPage.value = data.data;
        ScoreRuleList.value = data.data.records;
    });
  }
  else{
	  
    searchForm.value.page=value.page;
    searchForm.value.page_size=value.page_size
    getEmpScorePastSearch(searchForm.value).then(data=>{
		let item = data.data.records
		
		for(var i = 0;i <item.length;i++){
			if(item[i].correctedValue == null ){
				item[i].grade = (Number(item[i].score)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
			}else{
				item[i].grade = (Number(item[i].correctedValue)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
			}
			item[i].scorePercent += "%"
			item[i].assessorPercent += "%"
		}
      ScoreListPage.value = data.data;
      ScoreRuleList.value = data.data.records;
    })
  } 
}

const update = () => {
  searchForm.value.empId=""
  searchForm.value.empName=""
  
  getEmpScorePastPage(searchForm.value).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "更新成功",
        type: "success",
      });
	  
	  let item = data.data.records
	  
	  for(var i = 0;i <item.length;i++){
	  	if(item[i].correctedValue == null ){
	  		item[i].grade = (Number(item[i].score)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
	  	}else{
	  		item[i].grade = (Number(item[i].correctedValue)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
	  	}
	  	item[i].scorePercent += "%"
	  	item[i].assessorPercent += "%"
	  }
	  
	  
      ScoreListPage.value = data.data;
      ScoreRuleList.value = data.data.records;
	  
	  
    } else {
      ElMessage.error("更新失败");
    }
  });
};

const now = new Date();
const year = now.getFullYear();
// 获取当前月份（注意：月份从0开始，所以需要加1）
const month = now.getMonth() == 0? 12:now.getMonth();
// 格式化为 YYYY-MM 形式
const yearMonth = `${year}-${month < 10 ? '0' : ''}${month}`;

searchForm.value.time = yearMonth

const check = (time)=>{
	 return time.getTime() > new Date(new Date().toLocaleDateString()).getTime() - 31 * 24 * 3600 * 1000;
}

</script>
<style lang="scss" scoped>
.el-card {
  border-radius: 10px;
}
</style>
